package com.example.shortmessage.tips.broadcast;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.example.shortmessage.R;


/**
 * 状态通知
 * @author 27638
 */
public class NotificationStatus {
    private static NotificationStatus instance;

    public static synchronized NotificationStatus getInstance(Context context) {
        if (instance == null) {
            instance = new NotificationStatus(context);
        }
        return instance;
    }


    private final Context context;
    private NotificationChannel channel;
    private String channelId;
    private NotificationManagerCompat manager;

    private NotificationStatus(Context context) {
        this.context = context;
        createChannel();
        createNotificationManager();
    }

    /**
     * 创建渠道
     */
    private void createChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channelId = "channel-id";
            channel = new NotificationChannel(channelId,
                    "channel-name",
                    NotificationManager.IMPORTANCE_DEFAULT);
            //锁屏显示通知
            channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
            //桌面launcher消息角标
            channel.canShowBadge();
            //获取系统通知响铃声音的配置
            channel.getAudioAttributes();
            //获取通知渠道组
            channel.getGroup();
        }
    }

    /**
     * 设置渠道
     */
    private void createNotificationManager() {
        /**
         *  创建通知栏管理工具
         */
        manager = NotificationManagerCompat.from(context);
        /**
         *  绑定通知渠道
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            manager.createNotificationChannel(channel);
        }
    }

    /**
     * 设置配置
     * @return Notification
     */
    @NonNull
    private Notification getNotification() {
        /**
         *  实例化通知栏构造器
         */
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,channelId);
        /**
         *  设置Builder
         */
        builder.setSmallIcon(R.mipmap.ic_logo)
                //设置点击后自动清除
                .setAutoCancel(true)
                //设置标题
                .setContentTitle("消息发送失败")
                //设置状态栏里的通知时间
                .setWhen(System.currentTimeMillis())
                //设置通知栏中的文本内容
                .setContentText("消息发送失败");
                //启动意图
//                .setContentIntent(mainPendingIntent)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //builder的channelId需和下面channel的保持一致；
            builder.setChannelId(channelId);
        }
        /**
         * 返回 Notification
         */
        return builder.build();
    }

    /**
     * 发送通知
     */
    public void notification() {
        manager.notify(1, getNotification());
    }
}
